﻿using XamlCleaner.Core.Writer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml;
using System.IO;

namespace XamlCleaner.Core.Test
{


    /// <summary>
    ///This is a test class for XamlWriterTest and is intended
    ///to contain all XamlWriterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XamlWriterTest
    {

        string path = @"C:\Users\Miguel\Documents\Visual Studio 2010\Projects\CodePlex\XamlCleaner\XamlCleaner.Core.Test\bin\Debug\";

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            System.IO.File.Copy(string.Format("{0}OriginalTest.xaml", path),
               string.Format("{0}Test.xaml", path), true);
        }

        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            System.IO.File.Delete(string.Format("{0}Test.xaml", path));
        }

        #endregion


        /// <summary>
        ///A test for XamlWriter Constructor
        ///</summary>
        [TestMethod()]
        public void XamlWriter_Write_Overrides_PathFile_With_Document_When_FileNamePath_Exsist()
        {
            string SourcePath = string.Format("{0}Test.xaml", path); // TODO: Initialize to an appropriate value
            
            FileInfo fi = new FileInfo(SourcePath);
            
            XmlDocument document = new XmlDocument();
            document.Load(SourcePath);

            StreamWriter w = new StreamWriter(SourcePath, false);
            w.WriteLine("Archivo cambiado");
            w.Close();

            fi.Refresh();
            long lengthBefore=fi.Length;

            XamlWriter target = new XamlWriter(SourcePath, document);
            target.Write();

            fi.Refresh();
            long lengthAfter = fi.Length;

            Assert.IsFalse(lengthBefore.Equals(lengthAfter));
        }

        /// <summary>
        ///A test for XamlWriter Constructor
        ///</summary>
        [TestMethod()]
        public void XamlWriter_Write_Create_New_File_When_FileNamePath_NOT_Exsist()
        {

            string SourcePath = string.Format("{0}Test.xaml", path); // TODO: Initialize to an appropriate value

            File.Delete(string.Format("{0}BAD", SourcePath));

            XmlDocument document = new XmlDocument();
            document.Load(SourcePath);
            XamlWriter target = new XamlWriter(string.Format("{0}BAD", SourcePath), document);
            Assert.IsTrue(File.Exists(string.Format("{0}BAD", SourcePath)));
        }
    }
}
